//
//  SYBaiBanTimeView.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/12/28.
//  Copyright © 2019年 SanYi. All rights reserved.
//

#import "SYBaiBanTimeView.h"
#import "UILabel+SY.h"

@interface SYBaiBanTimeView ()
@property (strong, nonatomic) NSTimer *timer;
@property (strong, nonatomic) UILabel *titleLab;//上课时间lab
@property (strong, nonatomic) UILabel *timerLab;//时间lab
@property (assign, nonatomic) NSInteger totalTime;


@end


@implementation SYBaiBanTimeView

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initSubView];
    }
    return self;
}

- (void)initSubView {
    [self addSubview:self.titleLab];
    [self addSubview:self.timerLab];
    
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.left.equalTo(self.mas_left);
    }];
    
    [self.timerLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.left.equalTo(self.titleLab.mas_right).offset(10);
    }];
}
//开始计时
- (void)startTimer {
    [self.timer invalidate];
    self.timer = nil;
    self.timer =[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerClock:) userInfo:nil repeats:YES];
}



//结束计时
- (void)stopTimer {
    [self.timer invalidate];
    self.timer = nil;
}

- (void)timerClock:(NSTimer *)timer {
    self.totalTime++;
    _timerLab.text = [self getTimeIndex:self.totalTime];
}

- (NSString *)getTimeIndex:(NSInteger)totalTime {
    return [NSString stringWithFormat:@"%02ld:%02ld", totalTime / 60, totalTime % 60];
}


- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [UILabel titleColour:[UIColor colorWithHexString:@"#20b097"]
                              titleFount:15
                                pingfang:@"PingFang-SC-Regular"
                               alignment:NSTextAlignmentLeft
                               titleLine:1];
        _titleLab.text = @"上课时间：";
    }
    return _titleLab;
}

- (UILabel *)timerLab {
    if (!_timerLab) {
        _timerLab = [UILabel titleColour:[UIColor colorWithHexString:@"#666666"]
                              titleFount:15
                                pingfang:@"PingFang-SC-Regular"
                               alignment:NSTextAlignmentLeft
                               titleLine:1];
        _timerLab.text = @"00:00";
    }
    return _timerLab;
}


//暂停
- (void)zantingTimer {
    [_timer setFireDate:[NSDate distantFuture]];
}
//恢复
- (void)huifuTimer {
    [_timer setFireDate:[NSDate date]];
}

@end
